﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using SelectedRowsContainer.Models;

namespace SelectedRowsContainer
{
    public partial class Form1 : Form
    {
        private readonly List<UserModel> _userList;
        private BindingSource _bindingSource;
        private DataTable _dataTable;

        public Form1()
        {
            InitializeComponent();

            _userList = InitDataSource();
            dataGridView1.AutoGenerateColumns = false;

            #region 接入跨页选择

            ucSelectRows1.ShowColumnName = new List<DataGridViewColumn>()
            {
                colId,
                colUserName,
                colCreatedTime,
                colBalance
            };
            ucSelectRows1.GridView = dataGridView1;
            ucSelectRows1.CheckBoxColumn = colCheckbox;
            ucSelectRows1.KeyColumn = colId;

            #endregion
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindData(1);
        }

        private void BindData(int page)
        {
            if (page <= 0)
            {
                page = 1;
            }

            var list = _userList.Where(m => m.Id > (page - 1) * 30).Take(30).ToList();
            // dataGridView1.DataSource = list;

            var dt = Helper.ConvertToDataTable(list);
            var bs = new BindingSource();
            bs.DataSource = dt.DefaultView;
            dataGridView1.DataSource = bs;

            lblPage.Text = $@"当前：{page} 页";
        }

        private List<UserModel> InitDataSource(int maxRows = 1000)
        {
            var result = new List<UserModel>();

            for (int i = 0; i < maxRows; i++)
            {
                var random = new Random(i);
                var user = new UserModel
                {
                    Id = i + 1,
                    Balance = Math.Round(Math.Round(Convert.ToDecimal(random.NextDouble() * 1000), 2), 2),
                    CreatedTime = new DateTime(1900 + random.Next(60, 111), 3, 2, 3, 3, 3),
                    UserName = Helper.GenerateSurname()
                };

                result.Add(user);
            }

            return result;
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(lblPage.Tag?.ToString(), out var page))
                page = 1;
            if (page <= 1) return;

            page--;
            lblPage.Tag = page;
            BindData(page);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(lblPage.Tag?.ToString(), out var page))
                page = 1;

            page++;
            lblPage.Tag = page;
            BindData(page);
        }

        private void btnShowSelect_Click(object sender, EventArgs e)
        {
            var data = ucSelectRows1.GetSelectedRows();
            var json = string.Empty;
            if (data != null)
            {
                json = Newtonsoft.Json.JsonConvert.SerializeObject(data);   
            }

            var form = new frmShowContent(json)
            { 
                StartPosition = FormStartPosition.CenterScreen
            };

            form.Show();
        }
    }
}
